package com.shsoft.myThread;

import com.shsoft.colorfulcc.MainActivity;
import com.shsoft.common.Constant;

/**
 * 混色-呼吸灯 子线程
 */
public class MixBreathingThread extends Thread{
    private MainActivity activity;
    private int time; // 每次呼吸时间 s
    private int delay; // 有呼吸时间算得的亮度变更间隔 ms
    private int count; // 记录亮度变更次数
    private int breathingMode; // 记录呼吸模式
    private int bright;

    public MixBreathingThread(MainActivity activity) {
        this.activity = activity;
        time = Integer.parseInt(activity.breathing_time.getText().toString());
        bright = Integer.parseInt(activity.breathing_bright.getText().toString());
        delay = (time * 1000) / (bright * 2);
        count = 1;
        breathingMode = activity.breathingMode;
    }

    @Override
    public void run() {
        System.out.println("混色呼吸灯子线程运行");
        // 将灯珠的颜色全部换成要显示的颜色
        for(int i = 0; i < Constant.LIGHT_COUNT; i++){
            int colorIndex = activity.lights.get(i);
            if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                activity.lights_4display.put(i,new int[]{1,1,1});
            }else {
                activity.lights_4display.put(i,activity.colors.get(colorIndex));
            }
        }
        while (activity.isPlaying){
            switch (breathingMode){
                case Constant.BREATHING_MODE_D_L_D:
                    doWhileMode_d_l_d();
                    break;
                case Constant.BREATHING_MODE_L_D_L:
                    doWhileMode_l_d_l();
                    break;
                case Constant.BREATHING_MODE_L_D:
                    doWhileMode_l_d();
                    break;
                case Constant.BREATHING_MODE_D_L:
                    doWhileMode_d_l();
                    break;
                default:
                    break;
            }
            try {
                if(breathingMode == Constant.BREATHING_MODE_D_L_D || breathingMode == Constant.BREATHING_MODE_L_D_L){
                    sleep(delay);
                }else {
                    sleep(delay * 2);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
        }
    }


    private void doWhileMode_d_l_d(){
        // 让主线程刷新灯珠区域
        if(count <= bright){
            activity.alpha = count;
        }else {
            activity.alpha = bright - (count - bright);
        }
        if(activity.alpha * 2 > 255){
            activity.alpha = 255;
        }else {
            activity.alpha = activity.alpha * 2;
        }
        activity.refreshLightsDiv();
        count++;
        if(count == bright * 2){
            count = 1;
        }
    }
    private void doWhileMode_l_d_l(){
        // 让主线程刷新灯珠区域
        if(count <= bright){
            activity.alpha = (bright + 1) - count;
        }else {
            activity.alpha = count - (bright - 1);
        }
        if(activity.alpha * 2 > 255){
            activity.alpha = 255;
        }else {
            activity.alpha = activity.alpha * 2;
        }
        activity.refreshLightsDiv();
        count++;
        if(count == bright * 2){
            count = 1;
        }
    }
    private void doWhileMode_l_d(){
        // 让主线程刷新灯珠区域
        activity.alpha = (bright + 1) - count;
        if(activity.alpha * 2 > 255){
            activity.alpha = 255;
        }else {
            activity.alpha = activity.alpha * 2;
        }
        activity.refreshLightsDiv();
        count++;
        if(count == (bright + 1)){
            count = 1;
        }
    }
    private void doWhileMode_d_l(){
        // 让主线程刷新灯珠区域
        activity.alpha = count;
        if(activity.alpha * 2 > 255){
            activity.alpha = 255;
        }else {
            activity.alpha = activity.alpha * 2;
        }
        activity.refreshLightsDiv();
        count++;
        if(count == (bright + 1)){
            count = 1;
        }
    }
}
